// pages/takesTwo/twoWrong/twoWrong.js
const app=getApp();
let tool = require('../../../utils/tool.js');
Page({
  data: {
    innerAudioContext: new Object(),
    wordList:{},
    wordCss:{},
    clickList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    let returnWord = this.getData();
    returnWord.then(function(res){
      if (res.data.code == 1) {
        that.setData({ wordList: res.data.data });
        let tmparr = that.data.wordList;
        for (let i = 0; i < (that.data.wordList).length; i++) {
          tmparr[i].dis = 'none';
          tmparr[i].click = 0;
          tmparr[i].fontw = 'normal'
        }
        that.setData({
          wordList: tmparr
        })
      } else if (res.data.code == 10001) {
          wx.redirectTo({
              url: '/pages/login/login',
          })
      }
    })
   
   //音频插件处理
    this.data.innerAudioContext = wx.createInnerAudioContext()
    this.data.innerAudioContext.autoplay = false;
    this.data.innerAudioContext.loop = false;
    this.data.innerAudioContext.src = app.globalData.mp3.selected;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  clickWorld:function (e){
    this.data.innerAudioContext.stop();
    let tmp=new Object;
        tmp = this.data.wordList;
    let idx = e.currentTarget.dataset.idx;
    this.data.innerAudioContext.src = tmp[idx].vedio_url;
    this.data.innerAudioContext.play();
    if(tmp[idx].click==0){
      tmp[idx].fontw=800;
    }else if(tmp[idx].click==1){
   
      tmp[idx].dis='flex';
    } else if (tmp[idx].click == 2){
      tmp[idx].bgcolor = "#D6FFE7";
      tmp[idx].dis='none';
      this.data.clickList[idx]=idx;
    }else if (tmp[idx].click>2){
      tmp[idx].dis = (tmp[idx].dis=='none'?'flex':'none');
    }
    if (tmp[idx].click<=5){
      tmp[idx].click = parseInt(tmp[idx].click)+1;
    }
    this.setData({
      wordList:tmp
    });
  },
  goTo:function(){
    if(this.data.wordList.length==this.data.clickList.length){
      wx.navigateBack({
        delta: -1
      })
    }else{
      wx.showToast({
        title: '您还有未查看的单词',
        icon:'none'
      })
      return false;
    }
 
  },
  getData: function () { //从接口获取数据
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    let paramArr = {
      type:1,
      id: app.globalData.taskTwoUpresult.id,
      access_token: app.globalData.studentInfo.access_token,
    }
    let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
    let sign = rs.sign;
    let apiUrl = app.globalData.apiURL + "review/readerr?sign=" + sign;
    let takesPro = new Promise(
      function (resolve, reject) {
        wx.request({
          url: apiUrl,
          header: rs.header,
          data: rs.bodyParam,
          method: 'post',
          success(res) {
            resolve(res);
          },
          complete:function(){
            wx.hideLoading();
          }
        })
      }
    );
   
    return takesPro;
  }
})